let db = require('../db/index')
// 查询user表接口
const getUser = (req, res) => {
    let sql = 'select * from user order by id'
    db.query(sql, (err, result) => {
        if (err) {
            res.send({
                status: 400,
                message: '查询失败'
            })
            return
        } else {
            res.send({
                status: 200,
                message: '查询成功',
                result: result
            })
        }
    })
}
// 添加user表接口
const addUser = (req, res) => {
    let sql = 'insert into user (username,password,name,phone,imgUrl) values(?,?,?,?,?)'
    db.query(sql, [req.body.username, req.body.password, req.body.name, req.body.phone, req.body.imgUrl], (err, result) => {
        if (err) {
            res.send({
                status: 400,
                message: '添加失败'
            })
            return
        } else {
            res.send({
                status: 200,
                message: '添加成功',
                result: result
            })
        }
    })
}
// 更新user表接口
const updateUser = (req, res) => {
    let sql = 'update user set name=?,username=?,phone=?,imgUrl=? where id=?;'
    db.query(sql, [req.body.name, req.body.username, req.body.phone, req.body.imgUrl, req.body.id], (err, result) => {
        if (err) {
            res.send({
                status: 400,
                message: '更新失败'
            })
            return
        } else {
            res.send({
                status: 200,
                message: '更新成功',
                result: result
            })
        }
    })


}
// 根据用户的角色名称筛选数据
const filterUserData = (req, res) => {
    let sql = 'select * from user where name =?'
    db.query(sql, [req.body.name], (err, result) => {
        if (err) {
            res.send({
                status: 400,
                message: '查询失败'
            })
            return
        } else {
            res.send({
                status: 200,
                message: '查询成功',
                result: result
            })
        }
    })
}
// 根据传来的id, 和密码来更改用户密码
const setUserPwd = (req, res) => {
    let sql = 'update user set password=? where id =?'
    let sql1 = 'select * from user where id =? and password=?'
    console.log(req.body.oldPwd, req.body.id, req.body.newPwd)
    db.query(sql1, [req.body.id, req.body.oldPwd], (err, result) => {
        if (err) {
            res.send({
                status: 400,
                message: '用户不存在'
            })
        }
        if (result.length > 0) {
            db.query(sql, [req.body.newPwd, req.body.id], (err, result) => {
                if (err) {
                    res.send({
                        status: 400,
                        type: 'error',
                        message: '修改失败'
                    })
                    return
                } else {
                    res.send({
                        status: 200,
                        type: 'success',
                        message: '修改成功',
                        result: result
                    })
                }
            })
        } else {
            res.send({
                status: 202,
                type: 'error',
                message: '用户id或旧密码错误'
            })
        }
    })

}
module.exports = {
    getUser, updateUser, addUser, filterUserData, setUserPwd
}